package com.zzl.alligator.management.monitor.controller;

import com.zzl.alligator.common.constant.SystemConstants;
import com.zzl.alligator.common.enums.BusinessTypeEnum;
import com.zzl.alligator.common.util.ExcelUtils;
import com.zzl.alligator.framework.aspectj.annotation.Log;
import com.zzl.alligator.framework.base.controller.BaseController;
import com.zzl.alligator.framework.support.web.vo.JsonResult;
import com.zzl.alligator.management.monitor.domain.QrtzJobDetails;
import com.zzl.alligator.management.monitor.service.IQrtzJobDetailsService;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * (QrtzJobDetails)控制层
 *
 * @author zhouziliang
 * @since 2020-07-10
 */
@RestController
@RequestMapping("/monitor/job")
public class QrtzJobDetailsController extends BaseController {

    /**
     * 服务对象
     */
    @Autowired
    private IQrtzJobDetailsService qrtzJobDetailsService;

    /**
     * 分页查询所有数据ruhi
     * @param qrtzJobDetails 查询实体
     * @return 所有数据
     */
    @GetMapping
    public JsonResult findList(QrtzJobDetails qrtzJobDetails) {
        startPage();
        List<QrtzJobDetails> qrtzJobDetailsList = qrtzJobDetailsService.findQrtzJobDetailsList(qrtzJobDetails);
        return JsonResult.success(getDataTable(qrtzJobDetailsList));
    }

    @GetMapping("/{jobGroup}/{jobName}")
    public JsonResult getJob(@PathVariable("jobGroup") String jobGroup, @PathVariable("jobName") String jobName) throws SchedulerException {
        QrtzJobDetails job = qrtzJobDetailsService.findQrtzJobDetailsById(jobName, jobGroup);
        return JsonResult.success(job);
    }

    /**
     * 新增数据
     *
     * @param qrtzJobDetails 实体对象
     * @return 新增结果
     */
    @PostMapping
    @Log(title = "新增", businessType = BusinessTypeEnum.INSERT)
    public JsonResult add(@Validated @RequestBody QrtzJobDetails qrtzJobDetails) throws ClassNotFoundException,
            InstantiationException, SchedulerException, IllegalAccessException {
        qrtzJobDetailsService.save(qrtzJobDetails);
        return JsonResult.success();
    }

    /**
     * 修改数据
     *
     * @param qrtzJobDetails 实体对象
     * @return 修改结果
     */
    @PutMapping("/status")
    @Log(title = "修改任务状态", businessType = BusinessTypeEnum.UPDATE)
    public JsonResult update(@RequestBody QrtzJobDetails qrtzJobDetails) throws SchedulerException {
        String statue = qrtzJobDetails.getDealState();
        if (SystemConstants.NORMAL.equals(statue)) {
            qrtzJobDetailsService.resume(qrtzJobDetails);
        } else {
            qrtzJobDetailsService.pause(qrtzJobDetails);
        }
        return JsonResult.success();
    }

    /**
     * 删除数据
     *
     * @param jobGroup 任务组
     * @param jobName 任务名
     * @return 删除结果
     */
    @DeleteMapping("/{jobGroup}/{jobName}")
    @Log(title = "删除", businessType = BusinessTypeEnum.DELETE)
    public JsonResult delete(@PathVariable("jobGroup") String jobGroup,
                             @PathVariable("jobName") String jobName) throws SchedulerException {
        this.qrtzJobDetailsService.delete(jobGroup, jobName);
        return JsonResult.success();
    }
    
    /**
     * 导出数据
     * 
     * @param qrtzJobDetails 条件
     * @return 导出文件路径
     */
    @GetMapping("/export")
    @Log(title = "导出", businessType = BusinessTypeEnum.EXPORT)
    public JsonResult export(QrtzJobDetails qrtzJobDetails) {
        List<QrtzJobDetails> list = qrtzJobDetailsService.findQrtzJobDetailsList(qrtzJobDetails);
        ExcelUtils<QrtzJobDetails> util = new ExcelUtils<>(QrtzJobDetails.class);
        return util.exportExcel(list, "数据");
    }

    /**
     * 单次执行任务
     *
     * @param qrtzJobDetails 任务
     * @return 执行成功
     * @throws SchedulerException 执行异常
     */
    @PutMapping("/trigger")
    @Log(title = "单次执行", businessType = BusinessTypeEnum.OTHER)
    public JsonResult run(@RequestBody QrtzJobDetails qrtzJobDetails) throws SchedulerException {
        qrtzJobDetailsService.run(qrtzJobDetails);
        return JsonResult.success();
    }
}